<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-select
          v-model="query.orderStatus"
          placeholder="状态选择"
          class="handle-select mr10"
          @change="checkStatus"
          clearable
        >
          <el-option
            v-for="(item, index) in orderStatusArr"
            :key="index"
            :label="item"
            :value="index"
          ></el-option>
        </el-select>

        <!-- <el-input
          v-model="query.villageName"
          placeholder="请输入小区/村庄名称"
          class="handle-input mr10"
        ></el-input> -->
        <el-input
          v-model="query.alipayName"
          placeholder="姓名"
          class="handle-input mr10"
        ></el-input>
        <el-input
          v-model="query.alipayAccount"
          placeholder="支付宝账号"
          class="handle-input mr10"
        ></el-input>

        <!-- <el-input
          v-model="query.mobile"
          placeholder="请输入手机号"
          class="handle-input mr10"
          @keyup.native.enter="handleSearch"
        ></el-input> -->

        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>

        <el-button
          type="default"
          style="margin-bottom: 10px;"
          @click="handleSearch"
          >查询</el-button
        >
        <el-button
          type="default"
          style="margin-bottom: 10px;"
          @click="handleSearch"
          >全部通过</el-button
        >
        <el-button
          type="default"
          style="margin-bottom: 10px;"
          @click="handleExport"
          v-if="wmPermissions.includes('order:withdrawcash:export')"
          >导出</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="orderNo"
          label="订单号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="alipayName"
          label="姓名"
        ></el-table-column>

        <!-- <el-table-column
          align="center"
          prop="villageName"
          label="小区/村庄"
        ></el-table-column> -->

        <el-table-column
          align="center"
          prop="mobile"
          label="手机号"
        ></el-table-column>

        <el-table-column align="center" label="提现金额">
          <template slot-scope="scope">￥{{ scope.row.cash }}</template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="alipayAccount"
          label="支付宝账号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="createDate"
          label="发起时间"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="operationDate"
          label="操作时间"
        ></el-table-column>

        <el-table-column
          label="状态"
          prop="statusName"
          align="center"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="withdrawPoit"
          label="使用积分"
        ></el-table-column>

        <el-table-column v-if="flag" label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              v-if="
                scope.row.orderStatus == 0 &&
                  wmPermissions.includes('order:withdrawcash:update')
              "
              @click="handleEdit(scope.$index, scope.row, 1)"
              >打款成功</el-button
            >
            <el-button
              type="text"
              class="red"
              v-if="
                scope.row.orderStatus == 0 &&
                  wmPermissions.includes('order:withdrawcash:update')
              "
              @click="handleEdit(scope.$index, scope.row, 2)"
              >打款失败</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="失败原因" :visible.sync="editVisible" width="50%">
      <el-form ref="form" :model="form" label-width="auto">
        <el-input
          type="textarea"
          placeholder="请输入失败原因, 不超过120字"
          v-model="textarea"
          maxlength="120"
          show-word-limit
          :rows="8"
        >
        </el-input>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "withdrawalmanage",
  data() {
    return {
      wmPermissions: [],
      flag: true,
      textarea: "",
      value2: "",
      orderStatusArr: ["待审核", "已完成", "未成功"],
      query: {
        pageIndex: 1,
        pageSize: 20,
        orderStatus: "",
        // mobile: "",
        alipayName: "",
        alipayAccount: "",
        // villageName: "",
        // name: "",
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.getData();
    this.wmPermissions = this.permissions;
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/withdrawal-manage") {
        this.wmPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      // console.log(newVal, oldVal);
      this.wmPermissions = newVal;
    },
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      this.$axios
        .get("/order/withdrawcash/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            orderStatus: this.query.orderStatus,
            alipayName: this.query.alipayName.trim(),
            alipayAccount: this.query.alipayAccount.trim(),
            startDate: this.value2 ? this.value2[0].getTime() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
        })
        .then(({ data: res }) => {
          console.log("提现管理", res);
          if (res.code != 0) {
            return;
          }
          this.pageTotal = res.data.total || 0;
          res.data.list.map((item, index) => {
            item.createDate = this.dateFormat(item.createDate);
            item.operationDate =
              this.dateFormat(item.completeDate) ||
              this.dateFormat(item.updateDate);
            // item.updateDate = ;
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
            if (item.orderStatus == 0) {
              item.statusName = "审核中";
            } else if (item.orderStatus == 1) {
              item.statusName = "已完成";
            } else if (item.orderStatus == 2) {
              item.statusName = "提现失败";
            }
          });
          this.tableData = res.data.list;
        })
        .catch(() => {});
    },
    // 触发查询按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 导出
    handleExport() {
      this.$axios
        .get("/order/withdrawcash/export", {
          params: {
            orderStatus: this.query.orderStatus,
            alipayName: this.query.alipayName.trim(),
            alipayAccount: this.query.alipayAccount.trim(),
            startDate: this.value2 ? this.value2[0].getTime() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
          responseType: "blob",
        })
        .then((res) => {
          console.log(res);
          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch((error) => {
          this.$Notice.error({ title: "错误", desc: "网络连接错误" });
          // console.log(error);
        });
    },

    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    checkStatus(status) {
      console.log(status);
      if (status == 1 || status == 2) {
        this.flag = false;
      }
      if (status == "" || status == 0) {
        this.flag = true;
      }
      this.$set(this.query, "orderStatus", status);
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },

    // 编辑操作
    handleEdit(index, row, status) {
      console.log(index, row, status);
      this.idx = index;
      //   this.form = row;
      if (status == 2) {
        this.editVisible = true;
        this.status = status;
        this.id = row.id;
      } else if (status == 1) {
        this.$confirm("确定要操作吗？", "提示", {
          type: "warning",
        })
          .then(() => {
            this.$axios
              .put("/order/withdrawcash", {
                id: row.id,
                orderStatus: status,
              })
              .then(({ data: res }) => {
                if (res.code != 0) {
                  return;
                }
                this.getData();
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
              })
              .catch(() => {});
          })
          .catch(() => {});
      }
    },
    cancel() {
      this.editVisible = false;
      this.textarea = "";
    },
    // 保存编辑
    saveEdit() {
      if (this.textarea.trim().length == 0) {
        return this.$message.warning("不能为空");
      }
      this.editVisible = false;
      this.$axios
        .put("/order/withdrawcash", {
          id: this.id,
          orderStatus: this.status,
          memo: this.textarea,
        })
        .then(({ data: res }) => {
          if (res.code != 0) {
            return;
          }
          this.getData();
          this.$message.success(`修改第 ${this.idx + 1} 行成功`);
          this.textarea = "";
          this.id = "";
          this.status = "";
        })
        .catch(() => {});
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 160px;
  float: left;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  float: left;
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
